var superagent=require('superagent')
var cheerio=require('cheerio')
var async=require('async')
var fs = require('fs');
var xlsx = require('better-xlsx');





function saveExcel(data,typeName,topName){
    var file = new xlsx.File();
    var sheet = file.addSheet('Sheet1');
    for(var i=0;i<data.length;i++){
        var row = sheet.addRow();
        var cell1 = row.addCell();
        var cell2 = row.addCell();
        var cell3 = row.addCell();
        var cell4 = row.addCell();
        var cell5 = row.addCell();
        var cell6 = row.addCell();
        cell1.value = data[i]['skus'][0]['name'];
        cell2.value = data[i]['skus'][0]['desc'];
        cell3.value = typeName;
        cell4.value = data[i]['skus'][0]['std_unit_name'];
        cell5.value = data[i]['skus'][0]['std_sale_price']/100;
        cell6.value = data[i]['skus'][0]['img_url'];
    }
    
    file
      .saveAs()
      .pipe(fs.createWriteStream(topName + '/'+typeName+'.xlsx'))
      .on('finish', () => console.log('Done.'));
}




var urls=[]
var url='https://bshop.guanmai.cn/v587/product/category/get'

var jihaiUrl='https://bshop.guanmai.cn/v587/product/sku/get?level=2&category_id=B10134'
var cookie='cms_key=hmjzc; sessionid=yen86tfu7470b54wogjk3nauxmsm4rer; Hm_lvt_d02cd7e3028015e0088f63c017c81147=1507598680,1507703670; Hm_lpvt_d02cd7e3028015e0088f63c017c81147=1507707177'
superagent.get(url)
    .set("Cookie",cookie)
    .end(function(err,res){
        if(err)
            console.error('错误')
        else{
            var erji=JSON.parse(res.text)['data']
            for(var i=0;i<erji.length;i++){
                fs.mkdir(erji[i].name)
                for(var j=0;j<erji[i].children.length;j++){
                    urls.push({url:'https://bshop.guanmai.cn/v587/product/sku/get?level=2&category_id='+erji[i].children[j]['id'],name:erji[i].children[j]['name'],topName:erji[i]['name']})
                }
                
            }
            console.log(urls)
            getData()
        }
    })

function getData(){
    var concurrencyCount = 0
    var itemArr = []
    var fetchUrl = function(url, callback){
        // var delay = parseInt((Math.random() * 10000000) % 2000, 10)
        var delay = 1000
        concurrencyCount ++
        console.log('并发数：'+ concurrencyCount,'正在抓取：',url.url,'耗时：（ms）'+delay)
        superagent.get(url.url)
            .set("Cookie",cookie)
            .end(function(err,res){
                var data=JSON.parse(res.text).data
                saveExcel(data,url.name,url.topName)
            })
        setTimeout(function(){
            concurrencyCount --
            callback(null,url.url + 'html content')
        },delay)
    }

    async.mapLimit(urls,5,
        function(url, callback){
            fetchUrl(url, callback)
        },
        function(err, result){
            console.log('final:')
            console.log(result)
            console.log(itemArr)
        })
}